package de.tuhh.james.weka;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InstanceParser {
	
	private List<String> BSSIDList;
	private int missingValue = 0;
	private int strengthThreshold = 100;
	
	private static Logger logger =  LoggerFactory.getLogger(InstanceParser.class);
	
	public InstanceParser(List<String> BSSIDList){
		this.BSSIDList  = BSSIDList;
	}
	
	public int getMissingValue() {
		return missingValue;
	}

	public void setMissingValue(int missingValue) {
		this.missingValue = missingValue;
	}

	public int getStrengthThreshold() {
		return strengthThreshold;
	}

	/**
	 * Set the signal strength threshhold of Wi-Fi signal, signal strengh under greater then this value will be ignored.
	 * @param strengthThreshold
	 */
	public void setStrengthThreshold(int strengthThreshold) {
		this.strengthThreshold = strengthThreshold;
	}

	
	public String parse(final Map<String,String> instance){
		Set<String> keySet = instance.keySet();
		Map<String,String> instanceCopy = new TreeMap<String,String>();
		instanceCopy.putAll(instance);
		double max = strengthThreshold;//try to find the max signal strength in an instance
		logger.debug("Getting a new instance.");
		for(String key:keySet){
			if(!Transformer.CLASS_LABEL.equalsIgnoreCase(key)){
				double d = Math.abs(Double.parseDouble(instanceCopy.get(key)));
				logger.debug("Find a rssi, the value is "+d);
				if(d>strengthThreshold){
					instanceCopy.remove(key);
				}else{
					max = Math.min(max, d);
					logger.debug("max is now set to "+max);
				}
			}
			
		}
		max = Math.abs(max -100d);
		logger.debug("Find max for one instance "+max);
		String result = "";
		for(String BSSID:BSSIDList){
			if(instanceCopy.get(BSSID)==null){
				result+=missingValue;
				result+=",";
			}else{
//				result+=(instanceCopy.get(BSSID)+",");
				result+=(Math.abs(Integer.parseInt(instanceCopy.get(BSSID))-100)+",");
//				result+=(Math.abs(Double.parseDouble(instanceCopy.get(BSSID))-100)/max+",");
			}
		}
		result+=instanceCopy.get(Transformer.CLASS_LABEL);
		return result;
				
	}

}
